package com.nit.lab.controller.basic;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.nit.lab.dto.basic.GoodsDTO;
import com.nit.lab.dto.basic.GoodsListDTO;
import com.nit.lab.dto.system.TokenDTO;
import com.nit.lab.dto.utils.OpResultDTO;
import com.nit.lab.dto.utils.TableReqDTO;
import com.nit.lab.dto.utils.TableRspDTO;
import com.nit.lab.entity.basic.GoodsEntity;
import com.nit.lab.service.basic.GoodsListService;
import com.nit.lab.service.basic.GoodsService;
import com.nit.lab.utils.HttpRequestReader;
import com.nit.lab.utils.JWTUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping(value = "/api/goods")
public class GoodsController {

    @Resource
    private GoodsService goodsService;

    @Resource
    private GoodsListService goodsListService;

    private final Logger logger = LoggerFactory.getLogger(GoodsController.class);

    /**
     * @Description: 获取数据列表
     * @Author: wjm
     * @Date: 2022/06/28
     */
    @RequestMapping(value = "/query1", method = RequestMethod.POST)
    public TableRspDTO list4Table1(@RequestHeader(name = "Access-Token") String token, @RequestBody TableReqDTO tableReqDTO) {
        TableRspDTO tableRspDTO = new TableRspDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            tableRspDTO = goodsService.list4Table(tableReqDTO);
            List<GoodsDTO> records = tableRspDTO.getResult().getData();
//            System.out.println(records.get(0));
//            System.out.println(records);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return tableRspDTO;
    }


    /**
     * @Description: 新增记录
     * @Author: SN
     * @Date: 2019/01/31 11:02
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public OpResultDTO add( @RequestBody GoodsDTO goodsDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage(goodsService.add(goodsDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 更新从表记录
     * @Author: SN
     * @Date: 2019/01/31 11:02
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public OpResultDTO update(@RequestBody GoodsDTO goodsDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage(goodsService.update(goodsDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        System.out.println(opResult);
        return opResult;
    }

    /**
     * @Description: 删除记录从表记录
     * @Author: SN
     * @Date: 2019/01/31 11:02
     */
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public OpResultDTO remove(HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
            String ids = jsonRequest.getString("ids");
            opResult.setMessage(goodsService.remove(ids).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }


    /**
     * @Description: 下载文件
     * @Author: SN
     * @Date: 2019/01/31 11:02
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public OpResultDTO upload(MultipartFile file) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setResult(goodsService.upload(file));
            opResult.setMessage("success");
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description:  根据listId获取数据列表
     * @Author: wjm
     * @Date: 2022/06/28
     */
    @RequestMapping(value = "/queryByListId", method = RequestMethod.POST)
    public TableRspDTO list4TableByListId(@RequestHeader(name = "Access-Token") String token, @RequestBody TableReqDTO tableReqDTO) {
        TableRspDTO tableRspDTO = new TableRspDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            tableRspDTO = goodsService.list4TableByListId(tableReqDTO);
            //List<GoodsDTO> records = tableRspDTO.getResult().getData();
//            System.out.println(records.get(0));
//            System.out.println(records);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return tableRspDTO;
    }

    @RequestMapping(value = "/addAll", method = RequestMethod.POST)
    public OpResultDTO addAll( @RequestBody List<GoodsDTO> GoodsDTOs) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage(goodsService.addAll(GoodsDTOs).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 根据用户返回拥有藏品
     * @Author: SKW
     * @Date: 2019/01/31 11:02
     */
    @RequestMapping(value = "/getbyuser", method = RequestMethod.POST)
    public OpResultDTO getByUser(HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
            String owner = jsonRequest.getString("owner");
            opResult.setResult(goodsService.getByUser(owner));
            opResult.setMessage("success");
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }


}
